package com.feisi.controller;

import com.feisi.domain.Clue;
import com.feisi.domain.Customer;
import com.feisi.domain.User;
import com.feisi.service.ClueService;
import com.feisi.service.CustomerService;
import com.feisi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private UserService userService;

    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有线索
     *
     * @param request
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(HttpServletRequest request) {
        System.out.println("表现层:查询所有线索");
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        List<Clue> clues = clueService.findAll((page - 1) * limit, limit);
        int count = clueService.cluesTotal();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", clues);
        return map;
    }

    /**
     * 根据线索名称查线索
     */
    @RequestMapping("/findByName")
    public @ResponseBody
    Map<String, Object> findByName(String cl_name, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        List<Clue> clues = clueService.findByName(cl_name, (page - 1) * limit, limit);
        int count = clueService.cluesTotalByName(cl_name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", clues);
        return map;
    }

    /**
     * 根据根据用户id查出此用户负责的线索
     * 实现 我负责的线索 功能
     * @param userid
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByUserid")
    public @ResponseBody
    Map<String, Object> findByUserid(Integer userid, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {

        List<Clue> clues = clueService.findByUserid(userid, (page - 1) * limit, limit);
        int count = clueService.clueTotalByUserid(userid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", clues);
        return map;
    }


    /**
     * 新增线索
     * @param cl_name
     * @param cl_source
     * @param cl_phone
     * @param cl_e_mail
     * @param cl_industry
     * @param cl_level
     * @param cl_address
     * @param cl_next_time
     * @param cl_comment
     * @param cl_principal
     * @throws ParseException
     */
    @RequestMapping("/saveClue")
    public @ResponseBody
    Map<String, Object>  saveClue(String cl_name, String cl_source, String cl_phone, String cl_e_mail,
                         String cl_industry, String cl_level, String cl_creator,String cl_address, String cl_next_time,
                         String cl_comment, Integer cl_principal) throws ParseException {
        System.out.println("cl_creator="+cl_creator);
        Clue clue = new Clue();
        clue.setCl_name(cl_name);
        clue.setCl_source(cl_source);
        clue.setCl_phone(cl_phone);
        clue.setCl_e_mail(cl_e_mail);
        clue.setCl_industry(cl_industry);
        clue.setCl_level(cl_level);
        clue.setCl_address(cl_address);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        clue.setCl_create_time(df.format(new Date()));
        clue.setCl_next_time(cl_next_time);
        clue.setCl_comment(cl_comment);
        clue.setCl_creator(cl_creator);
        //User user = userService.findUserById(username, 0, 10).get(0);
        User user = userService.findUserById(cl_principal);
        clue.setUser(user);
        clueService.saveClue(clue);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 根据id删除合同
     * 批量删除
     * @param ids
     */
    @RequestMapping(value = "/deleteClue",method = {RequestMethod.POST})
    public @ResponseBody
    Map<String, Object> deleteClue(@RequestBody String[] ids){
        Integer[] id = new Integer[ids.length];
        System.out.println(ids.length);
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
            Integer idOne = Integer.valueOf(ids[i]);
            id[i]=idOne;

        }
        clueService.deleteClue(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }


    /**
     * 修改线索
     * @param id
     * @param cl_name
     * @param cl_source
     * @param cl_phone
     * @param cl_e_mail
     * @param cl_industry
     * @param cl_level
     * @param cl_address
     * @param cl_next_time
     * @param cl_comment
     * @param cl_principal
     * @throws ParseException
     */
    @RequestMapping("/updateClue")
    public @ResponseBody
    Map<String, Object>  updateClue(String id, String cl_name, String cl_source, String cl_phone, String cl_e_mail,
                         String cl_industry, String cl_level, String cl_address, String cl_next_time,
                         String cl_comment, Integer cl_principal) throws ParseException {

        System.out.println("id="+id+",cl_name="+cl_name+",cl_source="+cl_source+
                ",cl_phone="+cl_phone+",cl_e_mail="+cl_e_mail+",cl_industry="+cl_industry+
                ",cl_level="+cl_level+",cl_address="+cl_address+",cl_next_time="+cl_next_time+
                ",cl_comment="+cl_comment+",cl_principal="+cl_principal);
        Clue clue = new Clue();
        clue.setId(Integer.valueOf(id));
        clue.setCl_name(cl_name);
        clue.setCl_source(cl_source);
        clue.setCl_phone(cl_phone);
        clue.setCl_e_mail(cl_e_mail);
        clue.setCl_industry(cl_industry);
        clue.setCl_level(cl_level);
        clue.setCl_address(cl_address);
        clue.setCl_next_time(cl_next_time);
        clue.setCl_comment(cl_comment);
        //User user = userService.findUserById(username, 0, 10).get(0);
        User user = userService.findUserById(cl_principal);
        clue.setUser(user);
        clueService.updateClue(clue);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 线索转为客户
     *
     * @param cl_id 传一个线索的id过来
     */
    @RequestMapping("/clueToCustomer")
    public @ResponseBody
    Map<String, Object> clueToCustomer(@RequestParam("id") String cl_id) {
        System.out.println("线索id为"+cl_id);
        Integer id = Integer.valueOf(cl_id);

        Clue clue = clueService.findById(id);

        System.out.println("线索转客户，要删除的线索为："+clue);

        Customer customer = new Customer();
        customer.setCu_name(clue.getCl_name());
        customer.setCu_address(clue.getCl_address());
        customer.setCu_e_mail(clue.getCl_e_mail());
        customer.setCu_comment(clue.getCl_comment());
        customer.setCu_create_time(clue.getCl_create_time());
        customer.setCu_industry(clue.getCl_industry());
        customer.setCu_level(clue.getCl_level());
        customer.setCu_next_time(clue.getCl_next_time());
        customer.setCu_phone(clue.getCl_phone());
        customer.setCu_source(clue.getCl_source());
        customer.setCu_creator(clue.getCl_creator());
        customer.setUser(clue.getUser());
        //新增客户
        customerService.saveCustomer(customer);
        System.out.println("线索转为客户，添加的客户为"+customer);

        Integer[] ids = new Integer[cl_id.length()];
        System.out.println(ids.length);
        for (int i=0;i<ids.length;i++){
            ids[i]=id;
        }
        clueService.deleteClue(ids);


        Map<String, Object> map = new HashMap<>();
        map.put("msg", "success");
        return map;
    }


}
